/**************************************************************************//**
 * @item     CosyOS Kernel
 * @file     svs.h
 * @brief    中断异步服务
 * @author   迟凯峰
 * @version  V2.0.0
 * @date     2023.03.07
 ******************************************************************************/

#ifndef __SVS_H
#define __SVS_H

#if MCUCFG_MCULEVEL == __FEIPAOZI__
#define WRITE_VAR_WRIT_START  os_sign_write_var_writ = true
#define WRITE_VAR_WRIT_END    os_sign_write_var_writ = false
#define WRITE_MBX_WRIT_START  os_sign_write_mbx_writ = true
#define WRITE_MBX_WRIT_END    os_sign_write_mbx_writ = false
#define WRITE_MSG_HEAD_START  os_sign_write_msg_head = true
#define WRITE_MSG_HEAD_END    os_sign_write_msg_head = false
#else
#define WRITE_VAR_WRIT_START  do{}while(false)
#define WRITE_VAR_WRIT_END    do{}while(false)
#define WRITE_MBX_WRIT_START  do{}while(false)
#define WRITE_MBX_WRIT_END    do{}while(false)
#define WRITE_MSG_HEAD_START  do{}while(false)
#define WRITE_MSG_HEAD_END    do{}while(false)
#endif

#define sWRITE_VAR_WRIT(p)	\
	WRITE_VAR_WRIT_START;	\
	os_var_writ = p;	\
	WRITE_VAR_WRIT_END

#define sWRITE_MBX_WRIT(p)	\
	WRITE_MBX_WRIT_START;	\
	os_mbx_writ = p;	\
	WRITE_MBX_WRIT_END

#define sWRITE_MSG_HEAD(p)	\
	WRITE_MSG_HEAD_START;	\
	os_msg_head = p;	\
	WRITE_MSG_HEAD_END

/* 恢复任务 */
#define CASE_RESTASK	\
case __SVID_RESTASK__:	\
{	\
	tspTaskNode task_node = ((tspSV_OpeTask)svs)->TNOD;	\
	if(task_node->state & __SUSPENDED__)	\
	{	\
		task_node->state &= __RESUME__;	\
		if(!vScheduling_f)	\
		{	\
			if(task_node->state <= __FLOATING__ && task_node->TPL > vTASKING->TPL)	\
			{	\
				vScheduling_f = true;	\
			}	\
		}	\
	}	\
}	\
break;

/* 挂起任务 */
#define CASE_SUSTASK	\
case __SVID_SUSTASK__:	\
{	\
	tspTaskNode task_node = ((tspSV_OpeTask)svs)->TNOD;	\
	if(task_node->state < __SUSPENDED__)	\
	{	\
		task_node->state |= __SUSPENDED__;	\
		if(!vScheduling_f)	\
		{	\
			if(task_node == vTASKING)	\
			{	\
				vScheduling_f = true;	\
			}	\
		}	\
	}	\
}	\
break;

/* 删除任务 */
#define CASE_DELTASK	\
case __SVID_DELTASK__:	\
{	\
	tspTaskNode task_node = ((tspSV_OpeTask)svs)->TNOD;	\
	task_node->state = __DELETED__;	\
	if(!vScheduling_f)	\
	{	\
		if(task_node == vTASKING)	\
		{	\
			vScheduling_f = true;	\
		}	\
	}	\
}	\
break;

/* 设置优先级 */
#define CASE_SETTPL	\
case __SVID_SETTPL__:	\
{	\
	tspTaskNode task_node = ((tspSV_SetTPL)svs)->TNOD;	\
	u8 tpl = ((tspSV_SetTPL)svs)->TPL;	\
	vTPLBUF[task_node->TID] = tpl;	\
	vTPLSUM++;	\
	if(!vScheduling_f)	\
	{	\
		if(task_node == vTASKING || task_node->state <= __FLOATING__ && tpl > vTASKING->TPL)	\
		{	\
			vScheduling_f = true;	\
		}	\
	}	\
}	\
break;

/* 定时中断 */
#define CASE_TIMINT	\
case __SVID_TIMINT__:	\
{	\
	vTIMINT_STMR[((tspSV_TimInt)svs)->tmid] =	\
	vTIMINT_BUFF[((tspSV_TimInt)svs)->tmid] = ((tspSV_TimInt)svs)->tcyc;	\
}	\
break;

/* 定时查询 */
#define CASE_TIMQRY	\
case __SVID_TIMQRY__:	\
{	\
	vTIMQRY_STMR[((tspSV_TimQry)svs)->tmid] =	\
	vTIMQRY_BUFF[((tspSV_TimQry)svs)->tmid] = ((tspSV_TimQry)svs)->tcyc;	\
}	\
break;

/* 释放信号量 */
#define CASE_FREESEM	\
case __SVID_FREESEM__:	\
{	\
	if(((tspSV_Sem)svs)->sem->counter < ((tspSV_Sem)svs)->sem->max)	\
	{	\
		((tspSV_Sem)svs)->sem->counter++;	\
	}	\
}	\
break;

/* 全局变量写 */
#define CASE_WRITEVAR	\
case __SVID_WRITEVAR__:	\
{	\
	tspVarNode var_node = ((tspSV_Var)svs)->var_node;	\
	sWRITE_VAR_WRIT(var_node);	\
	var_node->var_size	\
	? __memcpy(var_node->gvar, var_node->lvar, var_node->var_size)	\
	: __strcpy(var_node->gvar, var_node->lvar);	\
	sWRITE_VAR_WRIT(NULL);	\
}	\
break;

/* 全局变量自运算 */
#define CASE_SELFOPE	\
case __SVID_SELFOPE__:	\
{	\
	tspVarNode var_node = ((tspSV_Ope)svs)->ope_node->var_node;	\
	__memcpy(var_node->lvar, var_node->gvar, var_node->var_size);	\
	sWRITE_VAR_WRIT(var_node);	\
	(*((tspSV_Ope)svs)->ope_node->fp)();	\
	sWRITE_VAR_WRIT(NULL);	\
}	\
break;

/* 发送邮件 */
#if SYSCFG_MAILBOX == __ENABLED__
#define CASE_SENDMAIL	\
case __SVID_SENDMAIL__:	\
{	\
	tspMbxNode mbx_node = ((tspSV_Mbx)svs)->mbx_node;	\
	sWRITE_MBX_WRIT(mbx_node);	\
	__memcpy(mbx_node->gmbx, mbx_node->lmbx, mbx_node->mbx_size);	\
	sWRITE_MBX_WRIT(NULL);	\
	*mbx_node->gnmf = mbx_node->lnmf;	\
}	\
break;
#else
#define CASE_SENDMAIL
#endif

#if SYSCFG_MSGQUEUE == __ENABLED__
/* 删除消息 */
#define CASE_DELMSG	\
case __SVID_DELMSG__:	\
{	\
	if(os_msg_head->msg_free == os_msg_head->msg_ptr)	\
	{	\
		__free(os_msg_head->msg_ptr);	\
		os_msg_head->msg_free = NULL;	\
	}	\
	else if(os_msg_head->msg_free != NULL)	\
	{	\
		*(bool *)os_msg_head->msg_free = true;	\
	}	\
	sWRITE_MSG_HEAD(os_msg_head->next);	\
	if(os_msg_head == NULL)	\
	{	\
		os_msg_tail = NULL;	\
	}	\
}	\
break;

/* 发送消息 */
#define CASE_SENDMSG	\
case __SVID_SENDMSG__:	\
{	\
	tspMsgNode msg_node = ((tspSV_Msg)svs)->msg_node;	\
	if(os_msg_tail != NULL)	\
	{	\
		os_msg_tail->next = msg_node;	\
	}	\
	os_msg_tail = msg_node;	\
	os_msg_tail->next = NULL;	\
	if(os_msg_head == NULL)	\
	{	\
		sWRITE_MSG_HEAD(os_msg_tail);	\
	}	\
}	\
break;

/* 拷贝消息 */
#define CASE_COPYMSG	\
case __SVID_COPYMSG__:	\
{	\
	tspMsgNode msg_node = ((tspSV_Msg)svs)->msg_node;	\
	msg_node->msg_free = __malloc(msg_node->msg_size ? msg_node->msg_size : __strlen(msg_node->msg_ptr) + 1);	\
	if(msg_node->msg_free == NULL)	\
	{	\
		vFault.malloc_fail_send_msg = true;	\
	}	\
	else	\
	{	\
		msg_node->msg_size	\
		? __memcpy(msg_node->msg_free, msg_node->msg_ptr, msg_node->msg_size)	\
		: __strcpy(msg_node->msg_free, msg_node->msg_ptr);	\
		msg_node->msg_ptr = msg_node->msg_free;	\
		if(os_msg_tail != NULL)	\
		{	\
			os_msg_tail->next = msg_node;	\
		}	\
		os_msg_tail = msg_node;	\
		os_msg_tail->next = NULL;	\
		if(os_msg_head == NULL)	\
		{	\
			sWRITE_MSG_HEAD(os_msg_tail);	\
		}	\
	}	\
}	\
break;
#else
#define CASE_DELMSG
#define CASE_SENDMSG
#define CASE_COPYMSG
#endif

/* 写标志组 */
#if SYSCFG_FLAGGROUP == __ENABLED__
#define CASE_WRITEGRP	\
case __SVID_WRITEGRP__:	\
{	\
	switch(((tspSV_GRP)svs)->size)	\
	{	\
		case +1: *(u8  *)((tspSV_GRP)svs)->group |= ((tspSV_GRP)svs)->value; break;	\
		case +2: *(u16 *)((tspSV_GRP)svs)->group |= ((tspSV_GRP)svs)->value; break;	\
		case +4: *(u32 *)((tspSV_GRP)svs)->group |= ((tspSV_GRP)svs)->value; break;	\
		case -1: *(u8  *)((tspSV_GRP)svs)->group &=~((tspSV_GRP)svs)->value; break;	\
		case -2: *(u16 *)((tspSV_GRP)svs)->group &=~((tspSV_GRP)svs)->value; break;	\
		case -4: *(u32 *)((tspSV_GRP)svs)->group &=~((tspSV_GRP)svs)->value; break;	\
	}	\
}	\
break;
#else
#define CASE_WRITEGRP
#endif

/* ISS_Handler */
#define sISS_Handler	\
switch(*(const u8 _SV_MEM_ *)svs)	\
{	\
	CASE_RESTASK	\
	CASE_SUSTASK	\
	CASE_DELTASK	\
	CASE_SETTPL	\
	CASE_TIMINT	\
	CASE_TIMQRY	\
	CASE_FREESEM	\
	CASE_WRITEVAR	\
	CASE_SELFOPE	\
	CASE_SENDMAIL	\
	CASE_DELMSG	\
	CASE_SENDMSG	\
	CASE_COPYMSG	\
	CASE_WRITEGRP	\
}



#endif
